package org.example.votingsystem.dao;

import org.example.votingsystem.account.Account;
import org.example.votingsystem.util.SqlManege;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AccountDao {
    //    查找用户名和密码是否匹配
    public boolean checkAccountIdAndPassword(String id, String password) throws SQLException {
        // TODO: implement
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM account WHERE id =? AND password =?");
        preparedStatement.setString(1, id);
        preparedStatement.setString(2, password);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet.next();
    }
//    查找用户信息
    public Account findAccountById(String id) throws SQLException {
        // TODO: implement
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM account WHERE id =?");
        preparedStatement.setString(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            Account account = new Account();
            account.setId(resultSet.getString("id"));
            account.setPassword(resultSet.getString("password"));
            account.setName(resultSet.getString("name"));
            return account;
        }
        return null;
    }
//    查找id 是否存在
    public boolean isIdExist(String id) throws SQLException {
        // TODO: implement
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM account WHERE id =?");
        preparedStatement.setString(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet.next();
    }

//    注册用户
    public void registerAccount(Account account) throws SQLException {
        // TODO: implement
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO account(id, password, name) VALUES(?,?,?)");
        preparedStatement.setString(1, account.getId());
        preparedStatement.setString(2, account.getPassword());
        preparedStatement.setString(3, account.getName());
        preparedStatement.executeUpdate();
        sqlManege.disconnect(connection, preparedStatement);
    }

//    public static void main(String[] args) {
//        Account account = new Account();
//        account.setId("123");
//        account.setPassword("123");
//        account.setName("123");
//        AccountDao accountDao = new AccountDao();
//        try {
//            accountDao.registerAccount(account);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//    }
}

